//
//  HNSH_ChatOpenRedAlert.m
//  NongYe
//
//  Created by 李宇廷 on 2023/2/6.
//

#import "HNSH_ChatOpenRedAlert.h"
#import "HNSH_PopupController.h"
#import "HNSH_ChatOpenRedDetailViewController.h"

@interface HNSH_ChatOpenRedAlert ()
/* nyPopView */
@property(nonatomic, strong) HNSH_PopupController *nyPopView;

/* headerImgV */
@property (nonatomic, strong) UIImageView *headerImgV;
/* nameLabel */
@property (nonatomic, strong) UILabel *nameLabel;
/* desLabel */
@property (nonatomic, strong) UILabel *desLabel;
/* openButton */
@property (nonatomic, strong) UIButton *openButton;
/* detailButton */
@property (nonatomic, strong) UIButton *detailButton;
/* closeButton */
@property (nonatomic, strong) UIButton *closeButton;
@end

@implementation HNSH_ChatOpenRedAlert

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configUI];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)showView {
    self.nyPopView = [[HNSH_PopupController alloc]init];
    self.nyPopView.dismissOnMaskTouched = YES;
    self.nyPopView.layoutType = PopupLayoutTypeCenter;
    self.nyPopView.slideStyle = PopupSlideStyleFade;
    [self.nyPopView presentContentView:self duration:0.25 springAnimated:YES inView:kSTWindow];
    [self clipCornerWithView:self.nyPopView.popupView cornerWidth:16 cornerType:UIRectCornerTopLeft|UIRectCornerTopRight];
}

- (void)closeView {
    [self.nyPopView dismissWithDuration:0.25 springAnimated:YES];
}

- (void)setRedInfo:(HNSH_ChatOpenRedInfoModel *)redInfo {
    _redInfo = redInfo;
    [self.headerImgV sd_setImageWithURL:[NSURL URLWithString:redInfo.avatar_url] placeholderImage:[UIImage imageNamed:@"login_home_center_icon"]];
    self.nameLabel.text = redInfo.nickname;
    self.desLabel.text = redInfo.tip_msg;
    self.openButton.hidden = !redInfo.show_open;
    self.detailButton.hidden = !redInfo.show_detail;
}

- (void)configUI {
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth-300)/2, (kScreenHeight-440)/2-34, 300, 440)];
    imgV.image = [UIImage imageNamed:@"chat_open_red_bg"];
    [self addSubview:imgV];
    imgV.userInteractionEnabled = YES;
    [imgV addSubview:self.openButton];
    [imgV addSubview:self.headerImgV];
    [imgV addSubview:self.nameLabel];
    [imgV addSubview:self.desLabel];
    [imgV addSubview:self.openButton];
    [imgV addSubview:self.detailButton];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX).offset(14);
        make.top.mas_equalTo(77);
        make.height.mas_equalTo(25);
    }];
    [self.headerImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.nameLabel.mas_left).offset(-4);
        make.centerY.equalTo(self.nameLabel.mas_centerY);
        make.width.height.mas_equalTo(24);
    }];
    
    [self.desLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(114);
    }];
    
    [self addSubview:self.closeButton];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.mas_equalTo(imgV.mas_bottom).offset(24);
        make.width.height.mas_equalTo(44);
    }];
    
}

#pragma mark lazy
- (UIImageView *)headerImgV {
    if (!_headerImgV) {
        _headerImgV = [[UIImageView alloc]init];
        _headerImgV.backgroundColor = [UIColor orangeColor];
        _headerImgV.layer.cornerRadius = 4.0f;
        _headerImgV.layer.masksToBounds = YES;
    }
    return _headerImgV;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.text = @"小六发出的红包";
        _nameLabel.textColor = [UIColor colorWithHexString:@"#F8E0A4"];
        _nameLabel.font = [UIFont boldSystemFontOfSize:18];
    }
    return _nameLabel;
}


- (UILabel *)desLabel {
    if (!_desLabel) {
        _desLabel = [[UILabel alloc]init];
        _desLabel.text = @"兔年如意兔年如意兔年 兔年如意兔";
        _desLabel.textColor = [UIColor colorWithHexString:@"#F8E0A4"];
        _desLabel.font = [UIFont systemFontOfSize:24];
        _desLabel.textAlignment = NSTextAlignmentCenter;
        _desLabel.numberOfLines = 0;
    }
    return _desLabel;
}

- (UIButton *)openButton {
    if (!_openButton) {
        _openButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _openButton.frame = CGRectMake(101, 282, 98, 98);
        [_openButton setImage:[UIImage imageNamed:@"chat_open_red_btn_open"] forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        [[_openButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf closeView];
            if (weakSelf.openRedBlock) {
                weakSelf.openRedBlock(weakSelf.redInfo);
            }
        }];
    }
    return _openButton;
}

- (UIButton *)detailButton {
    if (!_detailButton) {
        _detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _detailButton.frame = CGRectMake(0, 404, 300, 20);
        [_detailButton setTitle:@"查看领取详情" forState:UIControlStateNormal];
        [_detailButton setTitleColor:[UIColor colorWithHexString:@"#F8E0A4"] forState:UIControlStateNormal];
        _detailButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_detailButton setImage:[UIImage imageNamed:@"chat_open_red_icon_back"] forState:UIControlStateNormal];
        [_detailButton HNSH_layoutButtonWithEdgeInsetsStyle:HHZXButtonStyleTextLeft imageTitleSpace:0];
        __weak __typeof(&*self)weakSelf = self;
        [[_detailButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf closeView];
            HNSH_ChatOpenRedDetailViewController *vc = [[HNSH_ChatOpenRedDetailViewController alloc]init];
            vc.redId = weakSelf.redInfo.uid;
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        }];
    }
    return _detailButton;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:[UIImage imageNamed:@"chat_open_red_icon_close"] forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        [[_closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf closeView];
        }];
    }
    return _closeButton;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

